$(function () {
    $('.skin-minimal input').iCheck({
        checkboxClass: 'icheckbox-blue',
        radioClass: 'iradio-blue',
        increaseArea: '20%'
    });

    $("#form-admin-add").validate({
        rules:{
            realName:{
                required:true
            },
        	accountName:{
                required:true,
                remote: {
                    url: "/user/verifyaccountName",
                    type: "get",
                    data: {
                        accountName: function () {
                            return $("#accountName").val();
                        }
                    }
                }
            },
            password:{
                required:true
            },
            password2:{
                required:true,
                equalTo: "#password"
            },
            sex:{
                required:true
            },
            phone:{
                required:true,
                isPhone:true
            },
            email:{
                email:true
            }
        },
        messages : {
            realName:{
                required:"用户名称不能为空！"
            },
        	accountName : {
                remote: "登录名称已经被注册！",
                required: "登录名称不能为空！"
            },
            password : {
              required: "密码不能为空！"
            },
            password2:{
                required:"确认密码不能为空！",
                equalTo: "密码不一致"
            },
            sex:{
                required:"性别不能为空！"
            },
            phone:{
                required:"手机号码不能为空！",
                isPhone:"手机号码格式不正确！"
            },
            email:{
                email:"邮箱格式不正确！"
            }
        },
        onkeyup:false,
        focusCleanup:true,
        success:"valid",
        submitHandler:function(form){
            $(form).ajaxSubmit({
                type: 'post',
                url: "/user/add",
                dataType:"json",
                success: function(data){
                    if(data.code === "0000"){
                        succeedMessage("新增用户成功！");
                        let index = parent.layer.getFrameIndex(window.name);
                        parent.location.reload();
                        parent.layer.close(index);
                    }else {
                        errorMessage(data.message);
                    }
                }
            });
            return false; // 非常重要，如果是false，则表明是不跳转，在本页上处理，也就是ajax，如果是非false，则传统的form跳转。
        }
    });
});